﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UnityScriptEngine;
using UploadManager;
using UnityCore;

namespace MBBStoreClient
{

    public partial class frmUpload : Form
    {
        SynchronizeData sync = new SynchronizeData();
        UnityScriptEngine.Threader t = new UnityScriptEngine.Threader();

        UnityProcessor proc = new UnityProcessor();
        UnityScript uScript;
        UnityCore.Threader thd = new UnityCore.Threader();

        public frmUpload()
        {
            InitializeComponent();

            sync.ProcessCompleted += new UnityCore.CoreHandler(sync_ProcessCompleted);
            sync.ProcessError += new UnityCore.CoreHandler(sync_ProcessError);
            sync.ProcessProgress += new UnityCore.CoreHandler(sync_ProcessProgress);
            
            btnClose.Click += new EventHandler(btnClose_Click);
            panel2.EnabledChanged += new EventHandler(panel2_EnabledChanged);
            btnUpload.Click += new EventHandler(btnUpload_Click);
            btnView.Click += new EventHandler(btnView_Click);
            proc.ProcessCompleted += new ProcessHandler(proc_ProcessCompleted);
            proc.ProcessError += new ProcessHandler(proc_ProcessError);
        }

        void btnView_Click(object sender, EventArgs e)
        {
            if (cmbStore.Text != "")
            {
                uScript = new UnityScript("GetListDirectory", 1);
                uScript.Script_Add(Script.RFFPU_LIST, "" + cmbStore.Text);
                proc.Run(uScript);
                pnlMsg.Visible = true;
            }
        }

        void proc_ProcessError(string id, string desc)
        {
            thd.SafeCall(this, delegate
            {
                MessageBox.Show(desc);
            });
        }

        void proc_ProcessCompleted(string id, string desc)
        {
            thd.SafeCall(this, delegate
            {
                switch (id)
                {
                    case "GetListDirectory":
                        
                        proc.Data_Fetch(out uScript);
                        //dateList.Items.AddRange(uScript.ToList(1));

                        DataTable tblList = new DataTable();
                        tblList = uScript.ToDataTable();
                        string[] cashRegistry = new string[0];
                        string date = DateTime.Parse(txtDate.Text).Month.ToString();
                        for (int i = 0; i < tblList.Rows.Count; i++)
                        {
                            if (tblList.Rows[i][1].ToString().Contains("Cash Registry_"+date))
                            {
                                Array.Resize<string>(ref cashRegistry, cashRegistry.Length + 1);
                                cashRegistry[cashRegistry.Length - 1] = tblList.Rows[i][1].ToString();
                            }
                        }
                        if (cashRegistry.Length > 0)
                        {
                            dateList.Items.Clear();
                            dateList.Items.AddRange(cashRegistry);
                        }
                        else
                        {
                            dateList.Items.Clear();
                            dateList.Items.Add("No Records Found");
                        }

                        pnlMsg.Visible = false;
                        break;
                }
            });
        }

        void sync_ProcessProgress(string pcode, string msg)
        {
            t.SafeCall(this, delegate
            {
                lstUpdate.Items.Add(msg);
                lstUpdate.SelectedIndex = lstUpdate.Items.Count - 1;
            });
        }

        void sync_ProcessError(string pcode, string msg)
        {
            t.SafeCall(this, delegate
            {
                lstUpdate.Items.Add(msg);
                lstUpdate.SelectedIndex = lstUpdate.Items.Count - 1;
                MessageBox.Show(msg);
                prgBar.MarqueeAnimationSpeed = 0;
                prgBar.Refresh();
                btnClose.Enabled = true;
                grpDetail.Enabled = true;
            });
        }

        void sync_ProcessCompleted(string pcode, string msg)
        {
            t.SafeCall(this, delegate
            {
                switch (pcode)
                {
                    case "GetStores":
                        cmbStore.Items.AddRange(sync.stores);
                        break;

                    case "Download":
                        prgBar.MarqueeAnimationSpeed = 0;
                        btnClose.Enabled = true;
                        grpDetail.Enabled = true;
                        prgBar.Refresh();
                        MessageBox.Show("Download Completed");
                        break;
                }

            });
        }
       
        void btnUpload_Click(object sender, EventArgs e)
        {
            prgBar.MarqueeAnimationSpeed = 10;
            grpDetail.Enabled = false;
            btnClose.Enabled = false;
            if (chkAll.Checked == false)
            {
                downloadDetails d = new downloadDetails();
                d.date = txtDate.Text;
                d.remNode = cmbStore.Text;
                //
                sync.Download_02(d);
            }
            else
            {
                sync.Download(txtDate.Text);
            }

        }

        void panel2_EnabledChanged(object sender, EventArgs e)
        {
            if (!panel2.Enabled)
            {
                panel2.BackColor = Color.MidnightBlue;
            }
            else
            {
                panel2.BackColor = Color.RoyalBlue;
            }
        }

        void btnClose_Click(object sender, EventArgs e)
        {
            pnlForm.Dispose();
            this.Dispose();
        }

        public Panel PanelForm
        {
            get { return pnlForm; }
        }

        public void Initialize(string Title)
        {
            this.Select();

            UnityProcessor.Settings.RFFPU_Server = "ftp://www.malolosresortclubroyale.com";
            UnityProcessor.Settings.RFFPU_UID = "mbb@malolosresortclubroyale.com";
            UnityProcessor.Settings.RFFPU_PWD = "elayorbulc";

            sync.Stores();

            

            lblText.Text = Title;
            prgBar.MarqueeAnimationSpeed = 0;
            prgBar.Refresh();
        }
    }
}
